package com.evil.common.mongo.entity;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * 抽象双主键
 *
 * @author liyang
 * @date 2024-06-14 16:26
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AbstractDoubleName<NAME extends Serializable> implements Serializable {

    /**
     * 名称
     */
    @NotBlank(message = "name cant blank")
    private NAME singleName;

    /**
     * 名称
     */
    @NotBlank(message = "doubleName cant blank")
    private NAME doubleName;

    public static <NAME extends Serializable> AbstractDoubleName<NAME> buildSingle(NAME singleName) {
        return new AbstractDoubleName<>(singleName, null);
    }

    public static <NAME extends Serializable> AbstractDoubleName<NAME> buildDouble(NAME doubleName) {
        return new AbstractDoubleName<>(null, doubleName);
    }

    public static <NAME extends Serializable> AbstractDoubleName<NAME> build(NAME singleName, NAME doubleName) {
        return new AbstractDoubleName<>(singleName, doubleName);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AbstractDoubleName<?> that = (AbstractDoubleName<?>) o;

        if (!singleName.equals(that.singleName)) return false;
        return doubleName.equals(that.doubleName);
    }

    @Override
    public int hashCode() {
        int result = singleName.hashCode();
        result = 31 * result + doubleName.hashCode();
        return result;
    }

    public String print() {
        return String.format("%s-%s", this.singleName, this.doubleName);
    }
}
